const db = require('../db/index');

// 导入fs处理文件
const fs = require('fs');
// file_name
// file_url
// file_size
// upload_person
// upload_time
// download_numver


// 上传文件
exports.uploadFile = (req, res) => {
  let oldName = req.files[0].filename;
  let file_size = Math.round(req.files[0].size * 1 / 1024*100)/100;
  let upload_time = new Date();
  let download_number = 0;
  let newName = Buffer.from(req.files[0].originalname, 'latin1').toString('utf8');

  // 校验是否已存在该文件
  let sql1 = 'select * from files where file_name=?';
  db.query(sql1, newName, (err, results) => {
    if (err) return res.cc(err);
    if (results.length > 0) return res.cc('文件已存在');
    // fs.renameSync( oldPath, newPath )

    fs.renameSync(`./public/upload/${oldName}`, `./public/upload/${newName}`);
    // files 这是表名字
    const sql = 'insert into files set ?';
    db.query(sql, {
      file_name: newName,
      file_size,
      upload_time,
      download_number,
      file_url: `http://127.0.0.1:3000/upload/${newName}`
    }, (err, results) => {
      if (err) return res.cc(err)
      if (results.affectedRows !== 1) return res.cc('头像上传失败')
      res.send({
        status: 0,
        message: '文件上传成功',
        file_url: `http://127.0.0.1:3000/upload/${newName}`
      })
    })
  })
}

// 绑定文件上传者
exports.bindUploadPerson = (req, res) => {
  let {
    upload_person,
    file_url
  } = req.body;
  let sql = 'update files set upload_person=? where file_url=?';
  db.query(sql, [upload_person, file_url], (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '绑定成功'
    })
  })
}

// 更新下载量
exports.updateDownloadNumber = (req, res) => {
  let {
    id,
    download_number
  } = req.body;
  download_number = download_number * 1 + 1;
  let sql = 'update files set download_number=? where id=?';
  db.query(sql, [download_number, id], (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '更新成功'
    })
  })
}

// 下载文件
exports.downFile = (req, res) => {
  let sql = 'select file_url from files where id=?';
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      file_url: results[0].file_url
    })
  })
}

// 获取文件列表/总数
exports.getFileList = (req, res) => {
  let sql = 'select * from files';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results,
      total: results.length
    })
  })
}

// 获取文件分页列表
exports.getFilepages = (req, res) => {
  let pageSize = (req.body.pageSize - 1) * 10;
  let sql = `select * from files order by upload_time limit 10 offset ${pageSize}`;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 搜索文件
exports.searchFile = (req, res) => {
  let {
    file_name
  } = req.body;
  let sql = `select * from files where file_name like '%${file_name}%'`;
  db.query(sql, file_name, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 删除文件
exports.deleteFile = (req, res) => {
  let {
    id
  } = req.body;
  let sql = 'delete from files where id=?';
  db.query(sql, id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '删除成功'
    })
  })
}